package com.bbx.taxi.client.widget.pop;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;

import com.bbx.androidapi.util.DensityUtil;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Util.DateFormat;
import com.yanxuwen.pickerview.CustomPicker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * 作者：严旭文 on 2016/12/14 14:10
 * 邮箱：420255048@qq.com
 */
public class DatePicker2 {
    private String label_today="今天";
    private String label_tomorrow="明天";
    private String label_after_tomorrow="后天";

    private int max_day=5;
    private String text_year;
    private String text_month;
    private String text_day;
    private String text_next_year;
    private float textsize;
    private float buttonsize;
    private Context context;
    public static DatePicker2 mDatePicker;
    public static DatePicker2 getInstance(Context context) {
        if (mDatePicker == null) {
            mDatePicker = new DatePicker2(context);
            return mDatePicker;

        } else
            return mDatePicker;
    }
    public DatePicker2(Context context) {
        this.context = context;
        text_year=context.getString(R.string.year);
        text_month=context.getString(R.string.month);
        text_day=context.getString(R.string.day);
        text_next_year=context.getString(R.string.next_year);
        textsize= 14;
        buttonsize=12;
    }
    public void setMaxDay(int day){
        this.max_day=day;
    }
    /**
     * 数字选择器
     * @param start  起始数字
     * @param end    结束数字
     * @return
     */
    public  CustomPicker showNum(int start ,int end){
        return showNum("","",start,end);
    }

    /**
     * 数字选择器
     * @param title  标题
     * @param start  起始数字
     * @param end    结束数字
     * @return
     */
    public  CustomPicker showNum(String title,String notes,int start ,int end){
        List<String> list=new ArrayList<>();
        for(int i=start;i<=end;i++){
            list.add(i+"");
        }
        CustomPicker pvCustom = new CustomPicker(context);
        pvCustom.setCancelable(true);
        pvCustom.setTitle(title);
        pvCustom.setNotes(notes);
        pvCustom.setWeight((float)1.5,1,1);
        pvCustom.setCancelable(true);
        pvCustom.setData(list);
        pvCustom.setButtonSize(buttonsize);
        pvCustom.setTextSize(textsize);
        pvCustom.setLabels("");
        pvCustom.setCyclic(list.size()>7?true:false);
        return pvCustom;
    }
    public CustomPicker showDate(){
        return showDate("","");
    }
    public CustomPicker showDate(String title,String notes){
        List<String> list=new ArrayList<>();
        long Time = DateFormat.getTime();
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(Time);
        int mYear = mCalendar.get(Calendar.YEAR);
        int mMonth = mCalendar.get(Calendar.MONTH) + 1;
        int mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
        int mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
        int mMinute = mCalendar.get(Calendar.MINUTE);
        //第一个添加现在出发
        list.add(Value.now_time);
        //添加时间
        list.addAll(initDay(mYear, mMonth, mDay));
        //修改前三天后面为“今天”，“明天”，“后天”
        list.set(1,list.get(1)+label_today);
        list.set(2,list.get(2)+label_tomorrow);
        list.set(3,list.get(3)+label_after_tomorrow);
        String[] hour={"-","00","01","02","03","04","05","06","07","08","09","10","11","12","13","14",
                "15","16","17","18","19","20","21","22","23"};
        String minute[]=new String[]{"-","00","10","20","30","40","50"};
        CustomPicker pvCustom = new CustomPicker(context);
        pvCustom.setCancelable(true);
        pvCustom.setTitle(title);
        pvCustom.setNotes(notes);
        pvCustom.setWeight((float) 1.5, 1, 1);
        pvCustom.setCancelable(true);
        pvCustom.setData(list, Arrays.asList(hour), Arrays.asList(minute));
        pvCustom.setButtonSize(buttonsize);
        pvCustom.setTextSize(textsize);
        pvCustom.setLabels("");
        pvCustom.setCyclic(false,false,false);
        return pvCustom;
    }
    /**
     *
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }
    /**
     * curYear当前年， curMonth当前月（用于含有小时跟分）
     */
    private List<String>  initDay(int curYear, int curMonth, int mDay) {
        List<String> list = new ArrayList<String>();
        list.clear();
        int max=max_day;
        if(getDay(curYear, curMonth)<mDay+max-1){
            list=addDayList(curYear, curMonth, mDay, false, true);// 第一个月
            if( (curMonth + 1) > 12){
                // 次年
                int size=max-list.size();
                for (int i = 1; i <=size; i++) {
                    int month=(curMonth + 1) % 12;
                    list.add(text_next_year+(month==0?12:month )+ text_month + i+text_day);
                }
            }
            else{
                int size=max-list.size();
                for (int i = 1; i <=size; i++) {
                    int month=(curMonth + 1) % 12;
                    list.add((month==0?12:month ) + text_month + i+text_day);
                }
            }

        }
        else{
            for (int i = mDay; i <mDay+ max; i++) {
                list.add(curMonth + text_month + i+text_day);
            }
        }
        return list;
    }

    /**
     * curYear当前年， curMonth当前月（不包含）
     */
    private List<String> initDay2(int curYear, int curMonth, int mDay) {
        List<String> list = new ArrayList<String>();
        list.clear();
        int max=max_day;
        if(getDay(curYear, curMonth)<mDay+max-1){
            list=addDayList(curYear, curMonth, mDay, false, true);// 第一个月
            if( (curMonth + 1) > 12){
                // 次年
                int size=max-list.size();
                for (int i = 1; i <=size; i++) {
                    list.add(text_next_year+(curMonth + 1) % 12 + text_month + i);
                }
            }
            else{
                int size=max-list.size();
                for (int i = 1; i <=size; i++) {
                    list.add((curMonth + 1) % 12 + text_month + i);
                }
            }

        }
        else{
            for (int i = mDay; i <mDay+ max; i++) {
                list.add(curMonth + text_month + i);
            }
        }
        return list;
    }


    private List<String> addDayList(int Year, int Month, int Day, boolean isNextYear,
                            boolean isFirstMonth) {
        List<String> list = new ArrayList<String>();
        Month = (Month == 0) ? Month = 12 : Month;
        if (isNextYear) {
            // 次年
            for (int i = 1; i <= getDay(Year, Month); i++) {
                list.add(text_next_year + Month + text_month + i);
            }
        } else {
            for (int i = (isFirstMonth ? Day : 1); i <= getDay(Year, Month); i++) {
                list.add(Month + text_month + i);
            }
        }
        return list;
    }

    /**
     * 格式化控件的时间
     */
    public String onTimeFormat(String day,String hour,String  minute){
        long Time = DateFormat.getTime();
		Calendar mCalendar = Calendar.getInstance();
		mCalendar.setTimeInMillis(Time);
		int year = mCalendar.get(Calendar.YEAR);
        //判断含有次年则加1年
        if(day.contains(text_next_year)){
            year+=1;
        }
        //判断是否含有今天，明天，后天
        if(day.contains(label_today)||day.contains(label_tomorrow)||day.contains(label_after_tomorrow)||day.contains(text_next_year)){
            day= day.replace(label_today,"");
            day=day.replace(label_tomorrow,"");
            day=day.replace(label_after_tomorrow,"");
            day=day.replace(text_next_year,"");
        }
        day=day.replace(text_year,"");
        day=day.replace(text_month,"-");
        day=day.replace(text_day,"");

        return  year+"-"+day+" "+hour+":"+minute;
    }
}
